package cn.lblbc.downloadlib.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface DownloadTaskDao {
    @Query("SELECT * FROM download_tasks")
    List<DownloadTaskEntity> getAllTasks();

    @Query("SELECT * FROM download_tasks")
    LiveData<List<DownloadTaskEntity>> getAllTasksLiveData();

    @Query("SELECT * FROM download_tasks WHERE task_id = :taskId")
    DownloadTaskEntity getTaskById(long taskId);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertTask(DownloadTaskEntity task);

    @Update
    void updateTask(DownloadTaskEntity task);

    @Query("DELETE FROM download_tasks WHERE task_id = :taskId")
    void deleteTask(long taskId);

    @Query("UPDATE download_tasks SET status = :status, update_time = :updateTime WHERE task_id = :taskId")
    void updateTaskStatus(long taskId, int status, long updateTime);

    @Query("UPDATE download_tasks SET downloaded_size = downloaded_size + :downloadedSize, update_time = :updateTime WHERE task_id = :taskId")
    void updateTaskProgress(long taskId, long downloadedSize, long updateTime);
}